package yl.hs.bmipfull.service.laboratorydata.imple;

import org.springframework.beans.factory.annotation.Autowired;
import yl.hs.bmipfull.annotations.DBTable;
import yl.hs.bmipfull.daos.laboratory.ILaboratorySysTagsDao;
import yl.hs.bmipfull.daos.laboratory.ILaboratorySystemLogDao;
import yl.hs.bmipfull.daos.system.IDutyInfoDao;
import yl.hs.bmipfull.daos.system.IOrganizationInfoDao;
import yl.hs.bmipfull.pojo.laboratory.LaboratorySystemLogEntity;
import yl.hs.bmipfull.pojo.system.OrganizationInfoEntity;
import yl.hs.bmipfull.service.IBaseService;
import yl.hs.bmipfull.service.impl.BaseService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.utils.datastate.OrganizationUnitType;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/*
 * LaboratoryBaseService
 * @category
 * @author 谢景广
 * @date 2021-05-25
 */
public class LaboratorydataBaseService extends BaseService implements IBaseService {
    @Autowired
    ILaboratorySysTagsDao tagsDao;
    @Autowired
    IOrganizationInfoDao orgDao;

    @Autowired
    IDutyInfoDao dutyDao;
    @Autowired
    private ILaboratorySystemLogDao logDao;

    @Override
    public Integer writeLog(ActionType action, String dataSN, String content, String dataJson, Class<?> classType) {
        var user = HsApp.getCurrent();
        var log = new LaboratorySystemLogEntity();
        var userSN = HsApp.getClient();
        String userName = userSN;
        if (user != null) {
            userSN = user.getUserSN();
            userName = user.getUserName();
        }
        String objectName = "信息";
        DBTable dbTable = null;
        if (classType != null) {
            dbTable = classType.getAnnotation(DBTable.class);
        }


        if (dbTable != null) {
            objectName = dbTable.disName();
            log.setTypeCode(dbTable.mapCode());
            log.setTypeName(dbTable.disName());
        } else {
            log.setTypeName("系统写入");
            log.setTypeCode("SystemAutoWrite");
        }
        log.setUserSN(userSN);
        switch (action) {
            case PRINT -> {
                content = String.format("%s打印了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
            case CREATE -> {
                content = String.format("%s创建了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
            case UPDATE -> {
                content = String.format("%s更新了编号为%s的%s信息:%s", userName, dataSN, objectName, content);
                break;
            }
            case DELETE -> {
                content = String.format("%s删除了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
            case EXPORT -> {
                content = String.format("%s导出了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
            case IMPORT -> {
                content = String.format("%s导入了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
            case QUERY -> {
                content = String.format("%s查询了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
            default -> {
                content = String.format("%s操作了编号为%s的%s信息", userName, dataSN, objectName);
                break;
            }
        }
        log.setContent(content);
        log.setDataSN(dataSN);
        log.setActionType(action.code);
        log.setHandleTime(new Date());
        log.setUserName(userName);
        return logDao.insertNew(log);
    }

    @Override
    public Integer writeLogs(ActionType action, String dataSN, String content, String dataJson, Class<?> classType) {
        return null;
    }

    @Override
    public void forWriteLog(String[] snList, short state) {
        var user = HsApp.getCurrent();
        String content="";
        if (state == -2) {
            for (var str:snList) {
                writeLog(ActionType.UPDATE,str,user.getUserName()+"彻底删除了数据编号为"+str+"的数据信息","",null);
            }
        }else{
            for (var str:snList) {
                writeLog(ActionType.UPDATE,str,user.getUserName()+"恢复删除了数据编号为"+str+"的数据信息","",null);
            }
        }
    }

    public Integer writeLog(ActionType action, String[] snArray, Class<?> classType) {
        String objectName = "信息";
        var user = HsApp.getCurrent();

        DBTable dbTable = null;
        if (classType != null) {
            dbTable = classType.getAnnotation(DBTable.class);
        }
        var log = new LaboratorySystemLogEntity();
        if (dbTable != null) {
            objectName = dbTable.disName();
            log.setTypeCode(dbTable.mapCode());
            log.setTypeName(dbTable.disName());
        } else {
            log.setTypeName("系统写入");
            log.setTypeCode("SystemAutoWrite");
        }
        var userSN = HsApp.getClient();
        String userName = userSN;
        if (user != null) {
            userSN = user.getUserSN();
            userName = user.getUserName();
        }
        log.setUserSN(userSN);
        var content = String.format("%s删除了编号为(%s)的%s信息", userName, String.join(",", snArray), objectName);
        log.setContent(content);
        log.setDataSN(String.join(",", snArray));
        log.setActionType(action.code);
        log.setHandleTime(new Date());
        return logDao.insertNew(log);
    }
    public Integer writeLog(ActionType action, String sn, Class<?> classType) {
        String objectName = "信息";
        var user = HsApp.getCurrent();

        DBTable dbTable = null;
        if (classType != null) {
            dbTable = classType.getAnnotation(DBTable.class);
        }
        var log = new LaboratorySystemLogEntity();
        if (dbTable != null) {
            objectName = dbTable.disName();
            log.setTypeCode(dbTable.mapCode());
            log.setTypeName(dbTable.disName());
        } else {
            log.setTypeName("系统写入");
            log.setTypeCode("SystemAutoWrite");
        }
        var userSN = HsApp.getClient();
        String userName = userSN;
        if (user != null) {
            userSN = user.getUserSN();
            userName = user.getUserName();
        }
        log.setUserSN(userSN);
        var content = String.format("%s删除了编号为(%s)的%s信息", userName, sn, objectName);
        log.setContent(content);
        log.setDataSN(sn);
        log.setActionType(action.code);
        log.setHandleTime(new Date());
        return logDao.insertNew(log);
    }

    public List<String> getDepartArraySN() {
        var user = HsApp.getCurrent();
        var st = new ArrayList<String>();
        if (user != null) {
            var dutyList = getUserDutyList(user.getUserSN());
            if(dutyList.size() > 0){
                for (int i = 0; i < dutyList.size(); i++) {
                    var duty = dutyList.get(i);
                    if(!st.contains(duty.getUnitSN())){
                        st.add(duty.getUnitSN());
                    }
                }
            }
            else
            {
                st.add(user.getOrganizationCode());
            }
        }
        return st;
    }

    public List<String> getCompanySNList(){
        var user = HsApp.getCurrent();

        var s = coreTagDao.queryBySN(new String[]{user.getUserSN()});
        var orgCode = Tl.getDBMapCode(OrganizationInfoEntity.class);
        var unitSn = s.stream().filter(p -> p.getTagType().equals(orgCode) && p.getMainSN().equals(user.getUserSN())).map(p -> p.getTagSN()).collect(Collectors.toList());
        var orgList = orgDao.queryBySnArray(unitSn.toArray(new String[unitSn.size()]));
        var allOrgSNList = new ArrayList<String>();
        for (int i = 0; i < orgList.size(); i++) {
            var orgCodes = orgList.get(i).getParentCode().split(",");
            if (orgCodes.length>0){
                for (int j = 0; j < orgCodes.length; j++) {
                    if(!allOrgSNList.contains(orgCodes[j])){
                        allOrgSNList.add(orgCodes[j]);
                    }
                }
            }
        }

        var allOrgList = orgDao.queryBySnArray(allOrgSNList.toArray(new String[allOrgSNList.size()]));
        allOrgSNList.clear();
        if (allOrgList.size()>0){
            for (int i = 0; i < allOrgList.size(); i++) {
                var org = allOrgList.get(i);
                if(org.getOrganizationType() == OrganizationUnitType.Company.code){
                    if(!allOrgSNList.contains(org.getSn())){
                        allOrgSNList.add(org.getSn());
                    }
                }
            }
        }
        var userOrgCode = user.getOrganizationCode().split(",");
        for (int i = 0; i <userOrgCode.length; i++) {
            if(!allOrgSNList.contains(userOrgCode[i])){
                allOrgSNList.add(userOrgCode[i]);
            }
        }
        return  allOrgSNList;
    }
}
